********************************************************************************
* 
* Table A7: Selected Granular Cost Outcomes 
*
********************************************************************************



clear all 
cap log close 

*******
******* settings  
*******
** specify an output directory 
local fpath_output "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606"

** directory containing the main analytic file
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

** directory ccontaining himss supplemental variables 
local fpath_himss = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/himss/input/"

** directory containing physician flows supplemental data 
local fpath_physician_flows = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/physician_flows/output/"

** replication analysis directory 
local fpath_analysis = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/analysis_20230606/"


** lines w/ significant increase for acquirer: 
local cost_varlist "wlog_adj_gen_tot wlog_adj_gen_css wlog_adj_gen_medrec wlog_adj_gen_nurseadmin wlog_adj_gen_pharmacy wlog_adj_gen_cap_equipment  wlog_adj_gen_laundry"

*******
******* control which sections to run
*******
local prep_data =1 
local run_dd = 1


log using "`fpath_output'/tablea7_log", replace 


if `prep_data' == 1 {
*******
******* read and assemble the data 
*******
* bring in the hospital-year panel  
	use "`fpath_build_output'/addl_cost_vars.dta", clear 

	
	* clean up
	label var hospbd "Total beds"

	* reformat urbancbsa variable: 1 if rural 
	bys id: egen urban2 = max(urbancbsa)
	replace urbancbsa = urban2 
	replace urbancbsa = 1 - urbancbsa 
	label var urbancbsa "Share rural hospitals"

	* drop if missing hrrcode
	drop if missing(hrrcode)

	* numeric variable for the aha id 
	egen id2 = group(id)


	* total cost per adjusted discharge 
	gen adj_costs = totcost / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_costs "Adjusted costs per IP discharge (\$1 millions)" 
	winsor2 adj_costs, cuts(05 95) by(year)

	* total revenue per adjusted discharge 
	gen adj_revenue = income / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_revenue "Adjusted revenue per IP discharge (\$1 millions)"
	winsor2 adj_revenue, cuts(05 95) by(year)

	* log costs and revenue 
	gen ladjcosts_w = log((adj_costs_w))
	label var ladjcosts_w "Log(costs/bed)"

	gen ladjrev_w = log((adj_revenue_w))
	label var ladjrev_w "Log(revenue/bed)"

	* Winsorize dafny price index 
	winsor2 dafny_price, cuts(05 95) by(year)
	gen ldafny_price05 = log(dafny_price_w)
	label var ldafny_price05 "log(Dafny price index)"

	* winsorize profit margin in each year
	winsor2 profit_margin, cuts(05 95) by(year) label

	* capital investment 
	replace capinv_tot = capinv_tot/1000000
	label var capinv_tot "Capital investment \$millions"
	
	* winsorize and log  capital investment
	winsor2 capinv_tot, cuts(05 95) by(year) label
	gen lw_capinv_tot = log(1+capinv_tot_w)
	label var lw_capinv_tot "log(1+winsorized capital investment)"


	* scale FTE counts by beds 
	gen fte_per_bed = fte/hospbd
	label var fte_per_bed  "Full time employees per bed"

	* create log(fte)
	gen logfte = log(fte)
	label var logfte "log(FTE)"
	label var fte "Full Time Employees"

	* total general service costs 
	egen gen_tot = rowtotal(gen_*)


	********
	******** difference-in-difference analysis cleanup
	******** 
	**  restrict the sample: Legacy, Target, Other forprofits 
	keep if forprofit == 1 
	drop if target2 == 1 | acq_other == 1 
	replace acq_group = "Other for-profit" if missing(acq_group)

	label var ind08_11acq_legacy "Acquirer * 08-11"
	label var ind12_14target "Target * 12-14"

	* define post-treatment 
	gen postm = ( year >= 2008)
	gen postm_target = postm * target 
	gen postm_legacy = postm * acq_legacy 
	label var postm_target "Post 2008 * Target"
	label var postm_legacy "Post 2008 * Acquirer"

	* define interim
	gen interim = (year == 2007)
	gen interim_target = interim * target
	gen interim_legacy = interim * acq_legacy

	label var interim_target "Target 2007"
	label var interim_legacy "Acquirer 2007"

	* sanity check... 
	assert target2 + acq_other == 0 
	* event study
	* generate year dummies 
	tab year, gen(yr)

	*interact year dummies with acq legacy/ target indicators  
	foreach v of varlist yr* {
		gen target_`v' = target * `v'	
		gen legacy_`v' = acq_legacy * `v'

	}	

	* clean up the labels on group * year interactions 
	foreach v of varlist target_yr* {
		local num = subinstr("`v'", "target_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 2002
		label var `v' "Target * `num'"
	}


	foreach v of varlist legacy_yr* {
		local num = subinstr("`v'", "legacy_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 2002
		label var `v' "Acquirer * `num'"
	}	

	* drop dummy for 2006 (t-1)
	drop *yr4

	* gen year * hrr FEs
	egen yrhrr = group(year hrrcode)

	* gen year * HRR TREND
	gen yr_hrr_trend = year * hrrcode

	label var gen_admin "admin costs"
	label var gen_anesth "non-physician anesth. benefits"
	label var gen_cafeteria "cafeteria cost"
	label var gen_cap_building "building capital expenses"
	label var gen_cap_equipment "equipment capital expenses"
	label var gen_css "central services + supply costs"
	label var gen_dietary "dietary expenses (patient)"
	label var gen_ebd "EBD expenses"
	label var gen_housekeeping "housekeeping costs"
	label var gen_internresoth "intern + resident other costs"
	label var gen_internressal "intern + resident salaries"
	label var gen_laundry "laundry expenses"
	label var gen_maintenance "facility maintenance costs"
	label var gen_medrec "medical record maintenance costs"
	label var gen_nurseadmin "nursing admin salaries + benefits"
	label var gen_nursesch "nursing school expenses"
	label var gen_paramed "paramedical ed program expenses"
	label var gen_personnel "personnel maintenance (room + board)"
	label var gen_pharmacy "pharmacy costs"
	label var gen_plant "plant operating costs"
	label var gen_socservices "social service expenses"
	label var gen_tot "General Services"

	* lines w/ significant increase for acquirer: 
	* local cost_varlist " wlog_adj_gen_cap_equipment wlog_adj_gen_css wlog_adj_gen_laundry wlog_adj_gen_medrec wlog_adj_gen_nurseadmin wlog_adj_gen_pharmacy wlog_adj_gen_tot"

	foreach v of varlist gen_* {
	
		gen adj_`v' = `v'/((1000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
		*winsor2 adj_`v', cuts(05 95) by(year)
		drop if adj_`v' < 0
		winsor2 adj_`v', cuts(05 95) by(year)
		gen wlog_adj_`v' = log(1+adj_`v'_w)

		local lab: variable label `v'
	
		label var wlog_adj_`v' "1+(logged winsorized adj `lab')"
	
		*drop adj_`v'_w
	}

}



*******************************************************************************
** Difference-in-differences analysis 
*******************************************************************************
if `run_dd' == 1 {
	foreach v of varlist `cost_varlist' {
		di "`v'"
		qui eststo: reghdfe `v' postm_legacy postm_target interim_legacy interim_target  if forprofit == 1, absorb(year id2) vce(cluster id2)				


	}
	
	esttab, label se b(3) se(3) starlevels(* 0.10 ** 0.05 *** 0.010) nonotes replace title("General service costs") drop(interim* _cons) 

	*esttab using examine_costs/genserv_cost_decreasesv2.tex, label se b(3) se(3) starlevels(* 0.10 ** 0.05 *** 0.010) nonotes addnotes("Heteroskedasticity-robust standard errors clustered at the hospital level shown in parentheses." "Values are in hundreds." "All specifications include year FEs and hospital FEs." "Each observation is a hospital-year.") replace title("General service costs") drop(interim* _cons) 

	esttab using "`fpath_output'/TableA7.csv", label se b(3) se(3) starlevels(* 0.10 ** 0.05 *** 0.010) nonotes addnotes("Heteroskedasticity-robust standard errors clustered at the hospital level shown in parentheses." "Values are in hundreds." "All specifications include year FEs and hospital FEs." "Each observation is a hospital-year.") replace title("General service costs") drop(interim* _cons) 
	
}	
	


